package file1;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Test9 {
    public static void main(String[] args) throws IOException {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要扫描的目录路径：>>");
        String rootDirPath = scanner.next();
        File rootDir = new File(rootDirPath);
        if (!rootDir.isDirectory()) {
            System.out.println("您输入的扫描路径非法！");
            return;
        }
        System.out.println("请输入要删除的文件名！");
        String deleteFileName = scanner.next();
        List<File> result = new ArrayList<>();
        scanDir(rootDir, deleteFileName, result);
        for (File f : result) {
            System.out.println(f.getCanonicalPath() + "该文件是否确认删除？Y/N");
            String choice = scanner.next();
            if (choice.equals("Y") || choice.equals("y")) {
                f.delete();
                System.out.println(f.getCanonicalPath() + "该文件删除成功！");
            }
        }
    }

    private static void scanDir(File rootDir, String deleteFileName, List<File> result) throws IOException {
        File[] files = rootDir.listFiles();
        for (File file : files) {
            System.out.println("扫描了文件：" + file.getCanonicalPath());
            if (file.isFile()) {
                if (file.getName().equals(deleteFileName)) {
                    result.add(file);
                }
            } else if (file.isDirectory()) {
                scanDir(file, deleteFileName, result);
            }
        }
    }
}
